﻿using AutomatedTestDataManagement.Models;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;

namespace AutomatedTestDataManagement.Controllers
{
    public class SuiteController : Controller
    {
        private ApplicationTestDataEntities db = new ApplicationTestDataEntities();

        //
        // GET: /Suite/

        public ActionResult Index()
        {
            return View(db.TestSuites.ToList());
        }

        //
        // GET: /Suite/Details/5

        public ActionResult Details(int id = 0)
        {
            TestSuite testsuite = db.TestSuites.Find(id);
            if (testsuite == null)
            {
                return HttpNotFound();
            }
            return View(testsuite);
        }

        //
        // GET: /Suite/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Suite/Create

        [HttpPost]
        public ActionResult Create(TestSuite testsuite)
        {
            if (ModelState.IsValid)
            {
                db.TestSuites.Add(testsuite);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(testsuite);
        }

        //
        // GET: /Suite/Edit/5

        public ActionResult Edit(int id = 0)
        {
            TestSuite testsuite = db.TestSuites.Find(id);
            if (testsuite == null)
            {
                return HttpNotFound();
            }
            testsuite.TestSuiteScenarios = testsuite.TestSuiteScenarios.OrderBy(s => s.ScenarioNumber).ToList();
            return View(testsuite);
        }

        //
        // POST: /Suite/Edit/5

        [HttpPost]
        public ActionResult Edit(TestSuite testsuite)
        {
            if (ModelState.IsValid)
            {
                db.Entry(testsuite).State = EntityState.Modified;
                db.SaveChanges();
                foreach (TestSuiteScenario item in testsuite.TestSuiteScenarios)
                {
                    db.Entry(item).State = EntityState.Modified;
                    db.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            return View(testsuite);
        }

        //
        // GET: /Suite/Delete/5

        public ActionResult Delete(int id = 0)
        {
            TestSuite testsuite = db.TestSuites.Find(id);
            if (testsuite == null)
            {
                return HttpNotFound();
            }
            if (testsuite.TestSuiteScenarios.Count == 0)
            {
                return View(testsuite);
            }
            else
            {
                // create some view errors
                this.ModelState.AddModelError("", "This test suite has associated test scenarios. You must remove all linked test scenarios before deleting.");
                return View(testsuite);
            }
        }

        //
        // POST: /Suite/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            TestSuite testsuite = db.TestSuites.Find(id);
                db.TestSuites.Remove(testsuite);
                db.SaveChanges();
                return RedirectToAction("Index");
        }


        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}